package com.zoe.vhr.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zoe.vhr.model.Hr;
import com.zoe.vhr.util.R;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author Zoe.Zhang
 * @Date: 2021/07/26
 * @Description 登录成功处理器
 */
@Configuration
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse resp, Authentication authentication) throws IOException, ServletException {
        resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        PrintWriter writer = resp.getWriter();
        Hr hr = (Hr) authentication.getPrincipal();
        hr.setPassword(null);
        R<Hr> hrR = R.success(hr, "登录成功");
        String s = new ObjectMapper().writeValueAsString(hrR);
        writer.write(s);
        writer.flush();
        writer.close();
    }
}
